#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <iostream>

int main(int argc, char ** argv){
    cv::Mat frame;
    int cam_id = 0;
    double fps = 20;
    if(argc == 2){
        sscanf(argv[1], "%d", &cam_id);
    }
    cv::VideoCapture inVid(cam_id);
    if(!inVid.isOpened()){
        std::cout << "Can't Open Camera" << std::endl;
        return -1;
    }

//    double fps = (int) inVid.get(CV_CAP_PROP_FPS);
    int width = (int) inVid.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = (int) inVid.get(CV_CAP_PROP_FRAME_HEIGHT);

    cv::VideoWriter recVid("Recorded.avi",CV_FOURCC('F','F','D','S'),fps,cv::Size(width,height) ,true);
    if(!recVid.isOpened()){
        std::cout << "Can't Get Recorded" << std::endl;
        return -1;
    }
    cv::namedWindow("Camera");

    while(1){
        inVid >> frame;
        recVid << frame;
        cv::imshow("Camera", frame);
        if(cv::waitKey(1000/fps) >= 0)
            break;
    }
    inVid.release();
    return 0;
}
